package com.shgsh.server.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by pcc on 2015/8/13.
 */
public class ManageServerConClient {
    static Logger logger = LoggerFactory.getLogger(ManageServerConClient.class);

    //用户服务线程缓冲区 userId--thread
    public static HashMap<String,ServerConClientThread> hashMap = new HashMap<String,ServerConClientThread>();

    //添加一个客户端通信线程
    public static void addClientThread(String userId, ServerConClientThread cct) {
        logger.debug("add a thread for client id is " + userId);
        hashMap.put(userId,cct);

    }

    /**
     * 得到一个客户端通信线程
     * @param i
     * @return
     */
    public static ServerConClientThread getClientThread(String i){
        logger.debug(i + " client get a thread ");
        return  hashMap.get(i);
    }

    /**
     *
     * @return
     */
    public static List getAllOnLineUserid(){
        List list = new ArrayList();
        Iterator it = hashMap.keySet().iterator();
        while(it.hasNext()){
            list.add(it.next());
        }
        logger.debug("get all on line user");
        return list;
    }

    /**
     *
     * @param id
     * @return
     */
    public static boolean isOnline(String id){
        //使用迭代器完成
        Iterator<String> it=hashMap.keySet().iterator();
        while(it.hasNext()){
            String userId = it.next();
            if(userId.equals(id)){
                logger.debug(id + " user is online");
                return true;
            }
        }
        logger.debug(id + " user is offline");
        return false;
    }

}
